package com.lin.gobang;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lin.Sign.PP;
import com.lin.db.GameDB;

/**
 * Created by x1 on 2016/5/26.
 */
public class Signin extends Activity{
    private EditText user;
    private EditText password;
    private Button ok;
    private Button signup;
    public static int score;
    private GameDB db;
    private SQLiteDatabase dbRead,dbWrite;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        db = new GameDB(this);
        dbRead = db.getReadableDatabase();
        dbWrite = db.getWritableDatabase();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signin);
        user= (EditText) findViewById(R.id.row1);
        password= (EditText) findViewById(R.id.password);
        ok=(Button)findViewById(R.id.signin);
        db = new GameDB(this);
        dbRead = db.getReadableDatabase();
        dbWrite = db.getWritableDatabase();
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name=user.getText().toString();
                String pw=password.getText().toString();
                showAllUser();
                if(name.trim()==""||pw.trim()=="")
                {
                    String text1="输入的密码和密码不能为空，请重新输入";
                    Toast toasts=Toast.makeText(Signin.this,text1,Toast.LENGTH_SHORT);
                    toasts.show();
                }
                else
                {
                    String text2 = "Login fail!";
                    if(checkLogin(name,pw)){
                        text2="Login Succeed!";
                    }
                    Toast toasts=Toast.makeText(Signin.this,text2,Toast.LENGTH_SHORT);
                    toasts.show();
                    Intent intent=new Intent(Signin.this,PP.class);
                    startActivity(intent);
                }

            }
        });
        signup= (Button) findViewById(R.id.siginup);
        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent(Signin.this,Sign.class);
                startActivity(i);
            }
        });

    }

    //check login
    public boolean checkLogin(String username,String password){
        Cursor cursor = dbRead.query("Users",new String[]{"password"},"username='"+username+"'",null,null,null,null);
        while(cursor.moveToNext()){
            String pass = cursor.getString(cursor.getColumnIndex("password"));
            if(pass.equals(password)) return true;
        }


        return false;
    }

    public void showAllUser(){
        Cursor cursor = dbRead.query("Users",new String[]{"username,password,rank,score"},null,null,null,null,null);
        while(cursor.moveToNext()){
            String pass = cursor.getString(cursor.getColumnIndex("password"));
            String user = cursor.getString(cursor.getColumnIndex("username"));
            int rank=cursor.getInt(cursor.getColumnIndex("rank"));
            score=cursor.getInt(cursor.getColumnIndex("score"));
            System.out.printf("%s %s %d,%d\n",user,pass,rank,score);
        }
    }
}
